<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.* " %>
<jsp:useBean id="db" class="com.github.ginirohikocha.bean.Database"></jsp:useBean>

<!DOCTYPE HTML>
<html>
<head>
<title>温度监控系统-登陆账号</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="css/style-login.css" rel="stylesheet" type="text/css" media="all"/>
<script src="js/login.js"></script>
<%
	if (request.getParameter("logout") != null) {
		session.removeAttribute("username");
		session.removeAttribute("passowrd");
		response.sendRedirect("login.jsp");
	} else if (session.getAttribute("username") != null && session.getAttribute("password") != null) {
		Connection dbCon = db.getDbCon();
		Statement stmt = dbCon.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT * FROM user");
		while(rs.next()) {
			if (session.getAttribute("username").equals(rs.getString(2)) && session.getAttribute("password").equals(rs.getString(3))) {
				response.sendRedirect("index.jsp");
			}
		}
		rs.close();
		stmt.close();
		db.close();
	}
%>
<%
	boolean isNull = false, isFailed = false, isCode = true;
	String username = request.getParameter("username"), password = request.getParameter("password");

	request.setCharacterEncoding("utf-8");
	if (username != null && password  != null) {
		if (username.equals("用户名") || password.equals("")) {
			isNull = true;
		} else {
			String str1 = session.getAttribute("picode").toString(), str2 = request.getParameter("picode");
			if (!str1.equals(str2)) {
				isCode = false;
			} else {
				Connection dbCon = db.getDbCon();
				Statement stmt = dbCon.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT * FROM user");
				while(rs.next()) {
					if (username.equals(rs.getString(2)) && password.equals(rs.getString(3))) {
						session.setAttribute("username", username);
						session.setAttribute("password", password);
						response.sendRedirect("index.jsp");
						break;
					}
				}
				isFailed = true;
				rs.close();
				stmt.close();
				db.close();
			}
		}
	}
%>
<script type="text/javascript">
	function reload(t) {
		t.src = "./VerifyCodeServlet?flag=Math.random()";
	}
</script>
</head>
	<body>
		<div class="login">
			<h2>温度监控系统</h2>
			<div class="login-top">
				<h1>登陆账号</h1>
				<form action="login.jsp" method="post">
					<input name="username" type="text" value="用户名" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '用户名';}">
					<input name="password" type="password" value="password" onfocus="this.value = '';">
					<input name="picode" type="text" value="验证码" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '验证码';}">
			    <div class="login-submit">
			    	
			    	<img src="./VerifyCodeServlet" onClick="reload(this)">
			    	<input type="submit" value="登陆" >
			    </div>
			    </form>
			</div>
			<div class="login-bottom">
				<h3>
				<%
					if (isNull) {
						out.write("用户名或密码不能为空");
					}
					if (isFailed) {
						out.write("用户名或密码错误，请重试");
					}
					if (!isCode) {
						out.write("验证码错误，请重试");
					}
				%>
				</h3>
			</div>
		</div>
	</body>
</html>
